<template>
	<view class="container">
		<u-swipe-action :index="index" v-for="(res, index) in addressList" :key="res.address_id" @click="delAddress" :options="options">
			<view class="item" @click="selectAddress(res,index)">
				<!-- @tap="checkAddress(res)" -->
				<view class="top" >
					<view class="name">{{res.consignee}}</view>
					<view class="phone">{{res.mobile}}</view>
					<view class="tag">
						<text v-if="res.is_default" class="bg-base">默认</text>
					</view>
				</view>
				<view class="bottom">
					<view @tap="checkAddress(res)">
						<!-- {{res.merger_name}} {{res.address}} -->
						{{res.province}} {{res.city}} {{res.area}} {{res.address}}
					</view>
					<u-icon name="edit-pen" :size="40" color="#999999" @click="addAddress('edit', res)"></u-icon>
				</view>
			</view>
		</u-swipe-action>
		<view class="h200"></view>
		<button type="warning" shape="circle"  class="bottom-btn fs30 primarybtn"  @tap="addAddress('add')">
			<u-icon name="plus" color="#ffffff" :size="38"></u-icon>
			<text class="ml10">添加新地址</text>
		</button>
		<!-- 授权登录 -->
		<auth ref="auth"></auth>
	</view>
</template>

<script>
	import { mapState } from 'vuex'
	export default {
		data() {
			return {
				source: 0,
				from: '',
				type:'',
				addressList: [],
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#F65236'
					}
				}]
			}
		},
		computed: {
			...mapState(['hasLogin', 'userInfo'])
		},
		onLoad(options) {
			if (!this.hasLogin) {
				this.$refs.auth.open()
				return
			}
			this.source = options.source;
			this.from = options.from;
			this.type = options.type;
		},
		onShow() {
			this.getUseraddress();
		},
		methods: {
			selectAddress(res,index){
				if(this.type === "1"){
					
				 let pages = getCurrentPages() // 获取当前页面栈的实例，以数组形式按栈的顺序给出，第一个元素为首页，最后一个元素为当前页面。
				   let prevPage = pages[pages.length - 2] //上一页页面实例
				   prevPage.$vm.selectDDAddress(JSON.stringify(this.addressList[index])) // 给上一页绑定方法otherFun,传参地址id
					uni.navigateBack();
				}
			},
			checkAddress(res){
				
			},
			//获取地址列表
			getUseraddress() {
				this.$http.ajax({
					url: '/UserAddress/list',
					params: {
						userId: uni.getStorageSync('userInfo').id,
						page: 1,
						limit: 9999
					},
					isOP: 0
				}).then((res) => {
					console.log('res', res)
					if (res.code == 200) {
						this.addressList = res.data;
					} else {
						this.$api.msg(res.msg)
					}
				}).finally(()=>{
					
				})
			},
			addAddress(type, item) {
				let from = this.from;
				this.$api.toPage(`userAddressManage?from=${from}&type=${type}&data=${JSON.stringify(item)}`)
			}
		}
	}
</script>


<style lang="scss" scoped>
	.item {
		padding: 30rpx;

		.top {
			display: flex;
			font-weight: bold;
			font-size: 30rpx;

			.phone {
				margin-left: 60rpx;
			}

			.tag {
				display: flex;
				font-weight: normal;
				align-items: center;

				text {
					display: block;
					width: 60rpx;
					height: 34rpx;
					line-height: 34rpx;
					color: #ffffff;
					font-size: 20rpx;
					border-radius: 6rpx;
					text-align: center;
					margin-left: 30rpx;
				}

			}
		}

		.bottom {
			display: flex;
			margin-top: 20rpx;
			font-size: 28rpx;
			justify-content: space-between;
			color: #999999;
		}
	}

	
</style>
